import json
import random

def read_ua(dir_name):
    li = []
    a = 0
    with open(dir_name,'r',encoding='utf-8') as f:
        content = f.read()
        content_list = content.split('\n')
        for i in content_list:
            try:
                i = json.loads(i)
                userAgent = i.get('navigator.userAgent')
                li.append(userAgent)
            except Exception as e:
                a += 1
    return li
def read_ua_item(dir_name):
    li = []
    a = 0
    with open(dir_name, 'r', encoding='utf-8') as f:
        content = f.read()
        content_list = content.split('\n')
        for i in content_list:
            try:
                i = json.loads(i)
                userAgent = i.get('navigator.userAgent')
                if 'baiduboxapp' not in userAgent:
                    li.append(i)
            except Exception as e:
                a += 1
    return li
def make_js_str(item):
    js_str = 'var myScreen = {};\nfor (var i in window.screen) {\n\tmyScreen[i] = window.screen[i];\n};\nwindow.screen = myScreen;\n'
    for key,value in item.items():
        if 'navigator' in key:
            key_second = key.split('.')[-1]
            if key_second=='languages':
                js_navigator = 'Object.defineProperty(navigator,"%s",{get:function(){return %s;}});\n' %(key_second,value)
            else:
                js_navigator = 'Object.defineProperty(navigator,"%s",{get:function(){return "%s";}});\n' %(key_second,value)
            js_str += js_navigator
        elif 'screen' in key:
            for k,v in value.items():
                if v:
                    js_screen = '{}.{}={};\n'.format(key,k,v)
                else:
                    js_screen = '{}.{}="{}";\n'.format(key, k, v)
                js_str += js_screen
        else:
            js_else = '{}="{}";\n'.format(key,value)
            js_str += js_else
    return js_str

if __name__ == '__main__':
    a = read_ua_item('ua_m.txt')
    print(make_js_str(random.choice(a)))